﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;

namespace Golos.Tags.Http
{
    public partial class TagCacheServer
    {
        private void SearchQuestion(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("tag"))
                {
                    string tag = Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.QueryString["tag"]));
                    List<Guid> questionIds = tagsCache.GetQuestionIds(tag);
                    byte[] data = new byte[0];
                    if (questionIds.Count > 0)
                        using (MemoryStream stream = new MemoryStream())
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                foreach (var questionId in questionIds)
                                {
                                    writer.WriteLine(questionId);
                                }
                                writer.Flush();
                            }
                            data = stream.ToArray();
                        }
                    context.Response.ContentLength64 = data.LongLength;
                    context.Response.ContentEncoding = Encoding.Default;
                    context.Response.OutputStream.Write(data, 0, data.Length);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void GetAllTags(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                List<string> tags = tagsCache.GetAllTags();
                byte[] data = new byte[0];
                if (tags.Count > 0)
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            foreach (var tag in tags)
                            {
                                writer.WriteLine(tag);
                            }
                            writer.Flush();
                        }
                        data = stream.ToArray();
                    }
                context.Response.ContentLength64 = data.LongLength;
                context.Response.ContentEncoding = Encoding.Default;
                context.Response.OutputStream.Write(data, 0, data.Length);
                statusCode = (int)HttpStatusCode.OK;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void GetPopularTags(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                List<string> tags = tagsCache.GetPopularTags();
                byte[] data = new byte[0];
                if (tags.Count > 0)
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            foreach (var tag in tags)
                            {
                                writer.WriteLine(tag);
                            }
                            writer.Flush();
                        }
                        data = stream.ToArray();
                    }
                context.Response.ContentLength64 = data.LongLength;
                context.Response.ContentEncoding = Encoding.Default;
                context.Response.OutputStream.Write(data, 0, data.Length);
                statusCode = (int)HttpStatusCode.OK;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void GetQuestionTags(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("questionid"))
                {
                    Guid questionId = new Guid(context.Request.QueryString["questionid"]);
                    List<string> questionTags = tagsCache.GetQuestionTags(questionId);
                    byte[] data = new byte[0];
                    if (questionTags.Count > 0)
                        using (MemoryStream stream = new MemoryStream())
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                foreach (var tag in questionTags)
                                {
                                    writer.WriteLine(tag);
                                }
                                writer.Flush();
                            }
                            data = stream.ToArray();
                        }
                    context.Response.ContentLength64 = data.LongLength;
                    context.Response.ContentEncoding = Encoding.Default;
                    context.Response.OutputStream.Write(data, 0, data.Length);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }
    }
}
